package com.itic.mall.system.productview.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.itic.appbase.applications.sys.common.service.OutCommonService;
import com.itic.appbase.applications.sys.user.persistence.model.SysUser;
import com.itic.appbase.applications.sys.user.service.SysUserService;
import com.itic.appbase.framework.common.constants.Constants;
import com.itic.appbase.framework.common.constants.DBConst;
import com.itic.appbase.framework.common.constants.IticConst;
import com.itic.appbase.framework.common.constants.ResponseCode;
import com.itic.appbase.framework.common.controller.BaseController;
import com.itic.appbase.framework.exception.definition.ParameterErrorException;
import com.itic.appbase.framework.utils.StringHelper;
import com.itic.appbase.framework.utils.UserSessionHelper;
import com.itic.mall.system.marketuser.persistence.model.MarketUser;
import com.itic.mall.system.marketuser.service.MarketUserService;
import com.itic.mall.system.marketuserproduct.persistence.model.MarketUserProduct;
import com.itic.mall.system.marketuserproduct.persistence.model.MarketUserProductExample;
import com.itic.mall.system.marketuserproduct.service.MarketUserProductService;
import com.itic.mall.system.user.persistence.model.MallUser;
import com.itic.mall.system.user.service.MallUserService;


@Controller
@RequestMapping(value="/system/productview")
public class ProductViewController
     extends BaseController {
    @Autowired
    private MallUserService mallUserService;
    @Autowired
    private MarketUserProductService marketUserProductService;
    @Autowired
    private MarketUserService marketUserService;
    @Autowired
    private SysUserService sysUserService;

    @Autowired
   	protected OutCommonService outCommonService;
       
       //////////////////////////////////////////////////////常量区////////////////////////////////////////////////////////////////
       private static final String MODEL_NAME_CHINA = "店铺商品";//中文名
       private static final String MODEL_NAME＿ENGLISH = "productview";//英文名
       private static final String PAGE_COMMON_PATH = "/mall/system/productview/productview";//通用路径
       private static final String COMMON_URL = "/system/productview/";//通用路径

       //////////////////////////////////////////////////////页面跳转区////////////////////////////////////////////////////////////
      
       /*
        * 主页面
        */
       @RequestMapping(value="/init")
       public String init(Model model){
       	model.addAttribute("name_china", MODEL_NAME_CHINA);
       	model.addAttribute("name_english", MODEL_NAME＿ENGLISH);
       	model.addAttribute("common_url", COMMON_URL);
       	return PAGE_COMMON_PATH+"Main";
       }
       
       /*
        * 添加页面
        */
       @RequestMapping(value="/initAdd")
       public String initAdd(Model model){
       	model.addAttribute("name_china", MODEL_NAME_CHINA);
       	model.addAttribute("name_english", MODEL_NAME＿ENGLISH);
       	model.addAttribute("common_url", COMMON_URL);
       	return PAGE_COMMON_PATH+"Add";
       }
       
       /*
        * 编辑页面
        */
       @RequestMapping(value="/initEdit")
       public String initEdit(String id , Model model){
       	if( StringHelper.isNotBlank(id) ) {
       		MallUser temp = mallUserService.get(id);
       		model.addAttribute("editData",iticJsonMapper.toJson(temp));
       		model.addAttribute("name_china", MODEL_NAME_CHINA);
           	model.addAttribute("name_english", MODEL_NAME＿ENGLISH);
           	model.addAttribute("common_url", COMMON_URL);
       	}else {
               throw new ParameterErrorException();
           }
       	return PAGE_COMMON_PATH+"Edit";
       }
       
       /*
        * 查看页面
        */
       @RequestMapping(value="/initView")
       public String initView( Model model ){
    	    String userId = UserSessionHelper.get(Constants.USER_ID);
    	    SysUser sysUser = sysUserService.get(userId);
    	    String username = sysUser.getUsername();
    	    MarketUser user = marketUserService.getByInvitingCode(username);
    	    model.addAttribute("viewData",iticJsonMapper.toJson(user));
         	return PAGE_COMMON_PATH+"View";
       }
       
       
       //////////////////////////////////////////////////////业务代码区////////////////////////////////////////////////////////////
       
       /**
        * 添加模板管理数据
        * @param android
        * @return
        */
       @RequestMapping(value="/add")
       @ResponseBody
       public Map<String,String> add(MallUser MallUser){
       	
       	 Map<String, String> resultMap = new HashMap<String, String>();
            String result = ResponseCode.SERVER_ERROR.getCode();
            result = mallUserService.add(MallUser);
            
            resultMap.put(IticConst.RESULT, result);
            return resultMap;
       }
       
       
       /**
        * <p>Title:queryCondition </p>
        * <p>Description:封装查询条件 </p>
        * @param jzdw
        * @param jzdwExample
        * @return
        */
       private MarketUserProductExample queryCondition(HttpServletRequest request,MarketUserProduct temp) {
       	
       	   // 参数处理
    	   MarketUserProductExample example = new MarketUserProductExample();
           // 按创建时间倒序
           example.setOrderByClause("CREATE_TIME DESC");
           // 按排列号顺序
           String userId = UserSessionHelper.get(Constants.USER_ID);
   	       SysUser sysUser = sysUserService.get(userId);
   	       String username = sysUser.getUsername();
   	       MarketUser user = marketUserService.getByInvitingCode(username);
           com.itic.mall.system.marketuserproduct.persistence.model.MarketUserProductExample.Criteria criteria = example.createCriteria().andIsEffectiveEqualTo( DBConst.TRUE ).andUserIdEqualTo(user.getId());
           
           // 查询条件
           if ( null != temp ) {
       	       if( StringHelper.isNotBlank(temp.getName()) ){
       	    	   criteria.andNameLike("%"+temp.getName()+"%");
       	       }
           }
           return example;
       }
       
       /**
        * 
        * @param goldType
        * @param request
        * @return
        */
       @RequestMapping(value = "/list")
       @ResponseBody
       public Map<String, Object> list(MarketUserProduct product, HttpServletRequest request){
           // 声明返回对象
           Map<String, Object> resultMap = new HashMap<String, Object>();
           
           
           MarketUserProductExample example = this.queryCondition(request,product);
           // 封装分页参数
           example = this.handlePageQuery(request, example);
           
           
           // 获取信息总条数
           int total = marketUserProductService.count(example);
           
           // 获取数据
           List<MarketUserProduct> list = marketUserProductService.list(example);
           
           // 数据返回处理
           resultMap.put(IticConst.TOTAL_RECORDS, total);
           resultMap.put(IticConst.DISPLAY_RECORDS, total);
           resultMap.put(IticConst.RESULT, ResponseCode.SUCCESS.getCode());
           resultMap.put(IticConst.DATA, list);
           return resultMap;

       }
       
       /**
        * 编辑保存
        * @param MallUser
        * @return
        */
       @RequestMapping(value="/edit")
       @ResponseBody
       public Map<String,String> edit(String delFileIds , MallUser MallUser){
       
       	if( StringHelper.isNotBlank(delFileIds) ){
       		// 删除附件
       		outCommonService.deleteFiles(delFileIds);
       	}
       	
       	 Map<String, String> resultMap = new HashMap<String, String>();
            String result = ResponseCode.SERVER_ERROR.getCode();
            result = mallUserService.edit(MallUser);
            
            resultMap.put(IticConst.RESULT, result);
            return resultMap;
       }
       
       

       /**
        * 删除
        * 
        */
       @RequestMapping(value = "/del")
       @ResponseBody
       public Map<String, String> del(String id) {
           Map<String, String> resultMap = new HashMap<String, String>();
           String result = ResponseCode.SERVER_ERROR.getCode();


           if( StringHelper.isNotBlank(id) ){
               MallUser MallUser = new MallUser();
               MallUser.setId(id);
               result = mallUserService.delete(MallUser);
           }
           
           resultMap.put(IticConst.RESULT, result);
           return resultMap;
       }

}